package com.zzyl.controller;

import com.zzyl.service.AlertTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description AlertTaskController
 * @Author xulang
 * @Date 2025-09-16
 */
@RestController
@Slf4j
@RequestMapping("/alert-task")
public class AlertTaskController {
    @Autowired
    private AlertTaskService alertTaskService;

    /**
     * 定时执行报警检查任务（每分钟执行一次）
     */
    @Scheduled(fixedRate = 60000) // 60秒
    public void executeAlertTask() {
        try {
            log.info("定时任务触发：执行报警检查");
            alertTaskService.processAlertTask();
        } catch (Exception e) {
            log.error("定时执行报警检查任务失败", e);
        }
    }

    /**
     * 手动触发报警检查（用于测试）
     */
    @PostMapping("/trigger")
    public ResponseEntity<Map<String, Object>> triggerAlertTask() {
        Map<String, Object> response = new HashMap<>();
        try {
            log.info("手动触发报警检查任务");
            alertTaskService.processAlertTask();
            response.put("code", 200);
            response.put("message", "报警检查任务执行成功");
        } catch (Exception e) {
            log.error("手动触发报警检查任务失败", e);
            response.put("code", 500);
            response.put("message", "报警检查任务执行失败: " + e.getMessage());
        }
        return ResponseEntity.ok(response);
    }
}
